﻿using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace WPGPSData
{
    public partial class MainPage : PhoneApplicationPage
    {
        private GeoCoordinateWatcher position;

        public MainPage()
        {
            InitializeComponent();

            PrepareGeoCordinateWatcher();
        }

        private void PrepareGeoCordinateWatcher()
        {
            position = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
            position.MovementThreshold = 20;

            position.PositionChanged += watcher_PositionChanged;
            position.StatusChanged += watcher_StatusChanged;
            position.Start();
        }

        private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case GeoPositionStatus.Disabled:
                    this.StatusTextBlock.Foreground = new SolidColorBrush(Colors.Red);
                    break;
                case GeoPositionStatus.Initializing:
                    this.StatusTextBlock.Foreground = new SolidColorBrush(Colors.Orange);
                    break;
                case GeoPositionStatus.NoData:
                    this.StatusTextBlock.Foreground = new SolidColorBrush(Colors.Orange);
                    break;
                case GeoPositionStatus.Ready:
                    this.StatusTextBlock.Foreground = new SolidColorBrush(Colors.Green);
                    break;
                default:
                    this.StatusTextBlock.Foreground = new SolidColorBrush(Colors.White);
                    break;
            }

            this.StatusTextBlock.Text = e.Status.ToString();
        }

        private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            this.LatitudeTextBlock.Text = e.Position.Location.Latitude.ToString();
            this.LongitudeTextBlock.Text = e.Position.Location.Longitude.ToString();
            this.AltitudeTextBlock.Text = e.Position.Location.Altitude.ToString();
            this.SpeedTextBlock.Text = e.Position.Location.Altitude.ToString();
            this.CourseTextBlock.Text = e.Position.Location.Course.ToString();
            this.HorizantalAccuracyTextBlock.Text = e.Position.Location.HorizontalAccuracy.ToString();
            this.VerticalAccuracyTextBlock.Text = e.Position.Location.VerticalAccuracy.ToString();
            this.TimestampTextBlock.Text = e.Position.Timestamp.ToString();
        }

        private void AboutIconItem_Click(object sender, EventArgs e)
        {
            string author = System.Xml.Linq.XDocument.Load("WMAppManifest.xml").Root.Element("App").Attribute("Author").Value;
            string version = System.Xml.Linq.XDocument.Load("WMAppManifest.xml").Root.Element("App").Attribute("Version").Value;

            MessageBox.Show("GPS Data " + version + " © " + author);
        }
    }
}